package 华为OD机试真题2023.基础题;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Map;
import java.util.Scanner;

public class HJ24_找数字 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/基础题/input/24.txt"));
        int N = in.nextInt();
        int M = in.nextInt();
        int[][] map = new int[N][M];
        int[] date = new int[N * M];
        int k = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                map[i][j] = in.nextInt();
                date[k++] = map[i][j];
            }
        }
        int[][] res = new int[N][M];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                int minDistance = Integer.MAX_VALUE;
                for (int l = 0; l < N * M; l++) {
                    if (date[l] == map[i][j]) {
                        int x = l / 5;
                        int y = l % 5;
                        if (x == i && y == j) {
                            continue;
                        }
                        int nowDistance = Math.abs(x - i) + Math.abs(y - j);
                        if (nowDistance < minDistance) {
                            minDistance = nowDistance;
                        }
                    }
                }
                res[i][j] = minDistance == Integer.MAX_VALUE ? -1 : minDistance;
            }
        }
        System.out.println(Arrays.deepToString(res));
    }
}
